import API from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ticketList: [],
    selectTitle:'不支持退款'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  // 获取票种列表
  getAllTickts() {
    var data = {
      productid: wx.getStorageSync('product_id')
    }
    API.getAllTickts(data).then(res => {
      if (res.code == 0) {
        this.setData({
          ticketList: res.data
        })
      }
      console.log(res)
    })
  },
  // 添加票种
  addTicket() {
    wx.navigateTo({
      url: '/pages/editTicketClass/editTicketClass?type=1'
    })
  },
  // 编辑票种
  toEditTicket(e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/editTicketClass/editTicketClass?type=2&info=' + JSON.stringify(e.currentTarget.dataset.item)
    })
  },
  sortNumber(a, b) {
    return a - b
  },
  // 完成添加票种
  finshed() {

    var ticketList = this.data.ticketList
    if (ticketList.length > 0) {
      var price = []
      var setMoney = ""
      for (var k in ticketList) {
        price.push(ticketList[k].price)
      }
  price.sort(function(a,b){
          return a-b
      })

      if (price.length > 1) {

        setMoney =  price[0] + '~' + price.pop()
      } else {
        setMoney =  price[0]
      }
      wx.setStorageSync('setMoney', setMoney)
      wx.navigateBack({
        delta: 1
      })
    } else {
      wx.showModal({
        title: '',
        content: '请添加票种'
      })
    }
  },
  // 删除 票种
  delTicket(e) {
    console.log(e)
    var data = {
      id: e.currentTarget.dataset.ticktsid
    }
    API.deltickts(data).then(res => {
      console.log(res)
      if (res.code == 0) {
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(() => {
          this.getAllTickts()
        }, 2000)
      } else {
        wx.showToast({
          title: '删除失败',
          icon: 'success',
          duration: 2000
        })
      }
      console.log('删除票种')
    })
  },
  // 去退款页面
  toEditMoneyPage() {
    wx.navigateTo({
      url: '/pages/exitCostSet/exitCostSet'
    })
  },
  onShow: function () {
    this.getAllTickts()
    if (wx.getStorageSync('selectTitle')) {
      this.setData({
        selectTitle:wx.getStorageSync('selectTitle')
      })
    }
 
  },

})